<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  user_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-21.
//  Copyright 2011 lishuzu. All rights reserved.
// 


class Feed_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->lang->load('feed');
			$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
			$this->uid = $this->uri->segment(3) ? intval($this->uri->segment(3)) : $this->user['uid'];
		}
		
		//产生动态
		function feed_publish($id, $idtype, $add=0) {
			global $_SGLOBAL;

			$setarr = array();
			switch ($idtype) {
				// 发送我说feed
				case 'talkid':
					$this->db->where('talkid', $id);
					$query = $this->db->get('talk');
					if($value = $query->row_array()) {
						$setarr['icon'] = 'talk';
						$setarr['uid'] = $value['uid'];
						$setarr['name'] = $value['name'];
						$setarr['dateline'] = $value['dateline'];
						$setarr['title_template'] = $this->lang->line('feed_doing_title');
						$setarr['title_data'] = array('message'=>$value['message']);
						$setarr['body_template'] = '';
						$setarr['body_data'] = '';
						$setarr['id'] = $value['talkid'];
						$setarr['idtype'] = 'talkid';
					}
					break;
				// 发送作品feed
				case 'workid':
					$this->db->where('wid', $id);
					$query = $this->db->get('works');
					if($value = $query->row_array()) {
						//基本
						$setarr['icon'] = 'work';
						$setarr['id'] = $value['wid'];
						$setarr['idtype'] = $idtype;
						$setarr['uid'] = $value['uid'];
						$setarr['name'] = $value['name'];
						$setarr['dateline'] = $value['dateline'];

						//详细
						$url = base_url()."work/index/".$id;
						if($value['filepath']) {
							$setarr['image_1'] = get_workimg($value['filepath'], 'thumb', 'path');
							$setarr['image_1_link'] = $url;
						}
						//附加效果图
						$this->load->model('work_model');
						$workpics = $this->work_model->getworkpic($id);
						if ($workpics) {
							$i = 2;
							foreach ($workpics as $key => $pic) {
								if (($i <= 4) && $pic) {
									$setarr['image_'.$i] = get_workimg($pic['filepath'], 'thumb', 'path');
									$setarr['image_'.$i.'_link'] = $url;
								}else{
									break;
								}
								$i++;
							}
						}

						
						$setarr['title_data'] = array('subject' => "<a href=\"$url\">$value[title]</a>");
						$setarr['title_template'] = $this->lang->line('feed_blog');;
						$setarr['body_template'] = '<b>{subject}</b><br>{summary}';
						$setarr['body_data'] = array(
							'subject' => "<a href=\"$url\">$value[title]</a>",
							'summary' => getstr($value['description'], 150, 1, 1, 0, 0, -1)
						);
					}
					break;

				case 'picid':
					$plussql = $id>0?"p.picid='$id'":"p.uid='$_SGLOBAL[supe_uid]' ORDER BY dateline DESC LIMIT 1";
					$query = $_SGLOBAL['db']->query("SELECT p.*, a.friend, a.target_ids, s.username FROM ".tname('pic')." p
						LEFT JOIN ".tname('space')." s ON s.uid=p.uid
						LEFT JOIN ".tname('album')." a ON a.albumid=p.albumid WHERE $plussql");
					if($value = $_SGLOBAL['db']->fetch_array($query)) {
						if(empty($value['friend'])) {//隐私
							//基本
							$setarr['icon'] = 'album';
							$setarr['id'] = $value['picid'];
							$setarr['idtype'] = $idtype;
							$setarr['uid'] = $value['uid'];
							$setarr['username'] = $value['username'];
							$setarr['dateline'] = $value['dateline'];
							$setarr['target_ids'] = $value['target_ids'];
							$setarr['friend'] = $value['friend'];
							$setarr['hot'] = $value['hot'];
							//详细
							$url = "space.php?uid=$value[uid]&do=album&picid=$value[picid]";
							$setarr['image_1'] = pic_get($value['filepath'], $value['thumb'], $value['remote']);
							$setarr['image_1_link'] = $url;
							$setarr['title_template'] = '{actor} '.cplang('upload_a_new_picture');
							$setarr['body_template'] = '{title}';
							$setarr['body_data'] = array('title' => $value['title']);
						}
					}
					break;

				case 'sid':
					$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('share')." WHERE sid='$id'");
					if($value = $_SGLOBAL['db']->fetch_array($query)) {

						//基本
						$setarr['icon'] = 'share';
						$setarr['id'] = $value['sid'];
						$setarr['idtype'] = $idtype;
						$setarr['uid'] = $value['uid'];
						$setarr['username'] = $value['username'];
						$setarr['dateline'] = $value['dateline'];
						$setarr['hot'] = $value['hot'];

						//详细
						$url = "space.php?uid=$value[uid]&do=share&id=$value[sid]";

						$setarr['title_template'] = '{actor} '.$value['title_template'];
						$setarr['body_template'] =  $value['body_template'];
						$setarr['body_data'] = $value['body_data'];
						$setarr['body_general'] = $value['body_general'];
						$setarr['image_1'] = $value['image'];
						$setarr['image_1_link'] = $value['image_link'];
					}
					break;
			}

			if($setarr['icon']) {
				//数据处理
				$setarr['title_data'] = serialize($setarr['title_data']);//数组转化
				if($idtype != 'sid') {
					$setarr['body_data'] = serialize($setarr['body_data']);//数组转化
				}
				$setarr['hash_template'] = md5($setarr['title_template']."\t".$setarr['body_template']);//喜好hash
				$setarr['hash_data'] = md5($setarr['title_template']."\t".$setarr['title_data']."\t".$setarr['body_template']."\t".$setarr['body_data']);//合并hash

				$feedid = 0;
				if(!$add && $setarr['id']) {
					$wherearr = array(
							'id'=>$id,
							'idtype'=>'idtype',
						);
					$this->db->where($where);
					$result = $this->db->get('feed')->row_array();
					$feedid = $result['feedid'];
				}
				if($feedid) {
					$this->db->update('feed', $setarr, array('feedid'=>$feedid));
				} else {
					$this->db->insert('feed', $setarr);
				}
			}

		}
		
		// 得到uid feed
		function get_feeds($uids, $num = '50', $offset = ''){
			$data = $feeds =  array();
			if ($uids) {
				$offset = $offset ? $offset : 0;
				$this->db->where_in('uid', $uids);
				$data['count'] = $this->db->count_all_results('feed');
				if ($data['count']) {
					$this->db->where_in('uid', $uids);
					$this->db->order_by('dateline', 'DESC');
					$query = $this->db->get('feed', $num, $offset);
					foreach ($query->result_array() as $value) {
						$value['avatar_small'] = avatar($value['uid'], 'small');
						if ($value['image_1']) {
							$value['image_1'] = base_url().'upload/attachments/'.$value['image_1'];
						}
						if ($value['image_2']) {
							$value['image_2'] = base_url().'upload/attachments/'.$value['image_2'];
						}
						if ($value['image_3']) {
							$value['image_3'] = base_url().'upload/attachments/'.$value['image_3'];
						}
						if ($value['image_4']) {
							$value['image_4'] = base_url().'upload/attachments/'.$value['image_4'];
						}
						$feeds[] = $value;
					}
				}
				$data['list'] = $feeds;
				if ($data) {
					return $data;
				}else{
					return false;
				}
			}
			return false;
		}
		
		//得到feed
		function get_feed($id, $idtype){
			if ($id && $idtype) {
				$wherearr = array(
						'id'=>$id,
						'idtype'=>$idtype,
					);
				$this->db->where($wherearr);
				$feed = $this->db->get('feed')->row_array();
				return $feed;
			}
			return false;
		}
		
		//得到feed
		function delete_feed($id, $idtype){
			if ($id && $idtype) {
				$wherearr = array(
						'id'=>$id,
						'idtype'=> $idtype,
					);
				$this->db->where($wherearr);
				$this->db->delete('feed');
				return true;
			}
			return false;
		}
}
?>